package com.zh.webflux.auth;

/**
 * @author ZH
 * @date 15:46 2021-01-10
 */

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import reactor.core.publisher.Mono;

import java.util.Set;

/**
 * Web端用户
 * @author ZH
 * @date 13:20 2021-01-10
 */
@Slf4j
@AllArgsConstructor
public class WebUserDetailService implements ReactiveUserDetailsService {
//    private final UserService userService;
    private final PasswordEncoder passwordEncoder;

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        log.info("{}【Web端 有人Form登录】：username = {}", Thread.currentThread().getId(), username);

        var encodedPassword = passwordEncoder.encode("123456");

        Set<SimpleGrantedAuthority> authorities = Set.of(new SimpleGrantedAuthority("ROLE_USER"));

        var userInfo = UserInfo.builder()
                .username(username)
                .password(encodedPassword)
                .nickname(username)
                .avatar("")
                .authorities(authorities)
                .build();

        return Mono.just(userInfo);
    }

}

